#### Tables S5 and S6: partisan differences in support - norms/violence ####

# Libraries
# library(here)
# library(rio)
# library(tidyverse)
# library(ggthemes)
# library(srvyr)
# library(stargazer)

# data_pnas = import(here("Data","data_pnas.rds"))

violence_ind = paste0("violence",1:6,"re")
norm_ind = paste0("norm_",c("judges","polling","censorship","loyalty"),"re")

cs_summary_sig = data_pnas |> 
  filter(pid %in% c("Democrat","Republican")) |> # Just D/R
  select(pid, all_of(c(violence_ind, norm_ind)),
         uid, weight) |> 
  pivot_longer(-c("uid","pid","weight"), names_to = "var", values_to = "val") |> 
  group_by(var) |>
  nest() |> 
  mutate(mod = map(data, \(x){
    data_svy = as_survey_design(x, ids = uid, weights = weight)
    svyglm(val ~ pid, design = data_svy)
  })) |> 
  ungroup() |> 
  mutate(var = recode_factor(var,
                             norm_pollingre = "Polling Stations",
                             norm_loyaltyre = "Loyalty",
                             norm_judgesre = "Ignore Courts",
                             norm_executivere = "Exec. Power",
                             norm_censorshipre = "Censorship",
                             violence6re = "Murder",
                             violence5re = "Deadly weapon",
                             violence4re = "Arson",
                             violence3re = "Assault",
                             violence2re = "Vandalism",
                             violence1re = "Protest")) |> 
  select(-data)

stargazer(pull(cs_summary_sig, mod)[3:6],
          title = "Partisan Differences in Support: Violence",
          column.labels = as.character(pull(cs_summary_sig, var))[3:6],
          covariate.labels = c("Republican"),
          notes = "Estimated with survey weights and two-sided tests",
          star.cutoffs = c(0.05, 0.01, 0.001),
          omit.stat = c("aic","bic"),
          ci = T,
          type = "latex",
          out = here("Tables","Supplementary","table_s6.tex"),
          header = F)

stargazer(pull(cs_summary_sig, mod)[7:10],
          title = "Partisan Differences in Support: Norms",
          column.labels = as.character(pull(cs_summary_sig, var))[7:10],
          covariate.labels = c("Republican"),
          notes = "Estimated with survey weights and two-sided tests",
          star.cutoffs = c(0.05, 0.01, 0.001),
          omit.stat = c("aic","bic"),
          ci = T,
          type = "latex",
          out = here("Tables","Supplementary","table_s5.tex"),
          header = F)

rm(cs_summary_sig)